#!/bin/bash
##################################################################
#
# Script to check / audit basic system and report a detailed info
# from various system file, process and outputs
#
##################################################################
#
# Author: Bellamkonda Sudhakar
# License: GNU GPL v3
##################################################################
Version=0.1
UVersion=0.2
License='GNU GPL v3'
##################################################################
# Functions
##################################################################
function distribution {
OPDST='Unknown'
[[ $(echo ${BASH_VERSINFO[@]} | grep -c "slackware") ]] && OPDST='Slackware'
[[ $(grep -i fedora /proc/version) ]] && OPDST='Fedora'
[[ $(grep -i debian /proc/version) ]] && OPDST='Debian'
[[ $(grep -i centos /proc/version) ]] && OPDST='CentOS'
[[ $(grep -i suse /proc/version) ]] && OPDST='SUSE'
[[ $(grep -i rhel /proc/version) ]] && OPDST='RHEL'
[[ $(grep -i redhat /proc/version) ]] && OPDST='Redhat'
[[ $(grep -i ubuntu /proc/version) ]] && OPDST='Ubuntu'
echo "$OPDST" 
}
###################################################################
function depchkerr {
echo
echo "Dep check error - $Deperr"
echo
}
##################################################################
function depchk {
Depchk=0
Deperr=''
if [[ -z $(wc --version 2>/dev/null) ]]; then Depchk=1; Deperr='wc'; depchkerr; fi
if [[ -z $(dmesg 2>/dev/null) ]]; then Depchk=1; Deperr='dmesg'; depchkerr; fi
if [[ -z $(which which 2>/dev/null) ]]; then Depchk=1; Deperr='which'; depchkerr; fi
if [[ -z $(lspci 2>/dev/null) ]]; then Depchk=1; Deperr='lspci'; depchkerr; fi
if [[ -z $(awk --version 2>/dev/null) ]]; then Depchk=1; Deperr='awk'; depchkerr; fi
if [[ -z $(grep -V 2>/dev/null) ]]; then Dechk=1; Deperr='grep'; depchkerr; fi 
if [[ -z $(sed --version 2>/dev/null) ]]; then Depchk=1; Deperr='sed'; depchkerr; fi
if [[ -z $(mount -V 2>/dev/null) ]]; then Depchk=1; Deperr='mount'; depchkerr; fi
if [[ -z $(fdisk -v 2>/dev/null) ]]; then Depchk=1; Deperr='fdisk'; depchkerr; fi
if [[ -z $(du --version 2>/dev/null) ]]; then Depchk=1; Deperr='du'; depchkerr; fi
if [[ -z $(df --version 2>/dev/null) ]]; then Depchk=1; Deperr='df'; depchkerr; fi
if [[ -z $(sort --version 2>/dev/null) ]]; then Depchk=1; Deperr='sort'; depchkerr; fi
if [[ -z $(head --version 2>/dev/null) ]]; then Depchk=1; Deperr='head'; depchkerr; fi
if [[ -z $(which which 2>/dev/null) ]]; then Depchk=1; Deperr='which'; depchkerr; fi
if [[ -z $(netstat 2>/dev/null) ]]; then Depchk=1; Deperr='netstat'; depchkerr; fi
if [[ -z $(lsof 2>/dev/null) ]]; then Depchk=1; Deperr='lsof'; depchkerr; fi
if [[ -z $(ip -V 2>/dev/null) ]]; then Depchk=1; Deperr='ip'; depchkerr; fi
if [[ -z $(ss 2>/dev/null) ]]; then Depchk=1; Deperr='ss'; depchkerr; fi
if [[ -z $(ifconfig 2>/dev/null) ]]; then Depchk=1; Deperr='ifconfig'; depchkerr; fi
if [[ -z $(awk --version 2>/dev/null) ]]; then Depchk=1; Deperr='awk'; depchkerr; fi
if [[ -z $(tr --version 2>/dev/null) ]]; then Depchk=1; Deperr='tr'; depchkerr; fi
if [[ -z $(free -V 2>/dev/null) ]]; then Depchk=1; Deperr'free'; depchkerr; fi
if [[ $Depchk -eq 1 ]]; then exit 1; fi 
}
##################################################################
function usage {
echo
echo "Linux Easy Admin Utilities General System Auditor Version $Version - $License License"
echo 'Author: Bellamkonda Sudhakar
Usage: 
genadtchk [ -h | -c | -v ]
            -h Help
            -c Remove PID files
            -v Version'
echo
exit 0
}
###################################################################
function version {
echo
echo "Linux Easy Admin Utilities $UVersion - $License License"
echo "General Sysytem Auditor $Version"
echo 'Author: Bellamkonda Sudhakar'
echo
exit 0
}
##################################################################
function create_workspace {
mkdir $TMPFD 2>/dev/null 
}
##################################################################
function remove_workspace {
rm -Rf $TMPFD 2>/dev/null
}
##################################################################
function workspace {
if [ -d $TMPFD ] 
then
    remove_workspace
    create_workspace
else
    create_workspace
fi
}
##################################################################
function lpnt {
for r in {1..80..1}
do
   printf "%c" $1
done 
printf "\n"
}
##################################################################
function cleanup {
rm -f $CHKPID
echo
echo "PID files removed"
exit 0
}
##################################################################
function pause {
echo -n "Press enter to continue..."
read
}
##################################################################
function line {
echo >> $TMPFD/$AUDITFL
}
##################################################################
function idchk {
echo 
if (( UID ))
then
    echo
    echo "No root previleges"
    exit 1
fi
}
##################################################################
#Data
##################################################################
AUDITFL=$(hostname -s)
CHKPID='/var/run/genadtchk-ppid'
FLWPID='/var/run/genadtchk-fpid'
TMPFD='/tmp/genadtchk'
TMPFL1='tmpfl1'
TMPFL2='tmpfl2'
TMPFL3='tmpfl3'
OPSYS=$(uname -o)
OPKER=$(uname -s)
OPKRL=$(uname -r)
OPDST=$(distribution)
HSTNM=$( < /proc/sys/kernel/hostname )
DOMAIN=$( < /proc/sys/kernel/domainname ) 
#
depchk
workspace
if [[ "$OPDST" == "Slackware" ]] 
then
    $(ip -f link link | grep  '^[0-9]*: ' > $TMPFD/$TMPFL3)
    $(ip -f inet addr > $TMPFD/$TMPFL2)
else
    $(ip -d link | grep  '^[0-9]*: ' > $TMPFD/$TMPFL3)
    $(ip -d addr > $TMPFD/$TMPFL2)
fi
HNIC=$(lspci | grep -ic ethernet)
VNIC=$(grep -cv 'lo\|eth' $TMPFD/$TMPFL3)
LNIC=$(grep -c 'lo' $TMPFD/$TMPFL3)
TNIC=$(( HNIC + VNIC + LNIC ))
IP4E=$(grep -ic inet $TMPFD/$TMPFL2)
IP6E=$(grep -ic inet6 $TMPFD/$TMPFL2)
#
TTCP=$(ss -t | wc -l)
TTCPL=$(ss -t -l | wc -l)
TTCPC=$(ss -t -a | wc -l)
IPFW=$( < /proc/sys/net/ipv4/ip_forward)
IPMQ=$( < /proc/sys/net/ipv4/ip_dynaddr)
IPPROC=''
MJR=${OPKRL:0:1}
MIR=${OPKRL:2:1}
FWILBIN='None'
FWON='Off'
FWIN='False'
FWOT='False'
FWFW='False'
FWMQ='False'
NMSR=$(awk ' /nameserver/ { print $2; exit } ' /etc/resolv.conf )
DFGW=$(ip route | grep default | sed 's/default\|proto\|static//g')
HWRCTL=$(lspci | grep -iw "ide\|sata\|ata\|scsi" | wc -l)
CNTDSK=$(dmesg | grep -iw "ide\|sata\|scsi\|ata" | grep -w "link up" | wc -l) 
[[ "$OPDST" == "Slackware" ]] && CNTDSK=$(dmesg | grep -iw "ide\|sata\|ata\|scsi" | grep -w "ATA" | wc -l) 
HDTYPE=0
SDTYPE=0
SDD=0
HDD=0
RCD=0
SMD=0
DA=(a b c d e f g h i j k l m n o p)
FSTOT=$(mount | grep ^/dev/ | wc -l)
FRMFSTAB=$(grep -v ^# /etc/fstab | grep -v "tmpfs\|devpts\|sysfs\|proc\|swap" | sed '/^$/d' | wc -l)
PROCFS=$(ls /proc/fs)
##################################################################
#Main Program
##################################################################
clear
if [[ -n $1 ]] ; then
  case "$1"  in 
      -c ) idchk
           cleanup
           exit 0
           ;;
      -h ) usage
           exit 0
           ;;
      -v ) version
           exit 0
           ;;
       * ) echo "Invalid Option try genadtchk -h, for now continuing with no option"
           pause
           ;;
  esac
fi
idchk
#
if [ -f $CHKPID ] 
then
    echo " General Auditor running with PID $( < $CHKPID)"
    echo " To clear the PID files use the -c option"    
    exit 1
else
    echo $$ > $CHKPID
fi
#
case "$MJR" in
    "2" )
        case "$MIR" in
             "0" | "1" )
                 FWILBIN='ipfwadm';
                 IPPROC='ip_[mfp][ao][rs][wqt][qha]*';;
             "2" | "3" )
                 FWILBIN='ipchains';
                 IPPROC='ip_[fm][wa][cns]*';;
             * ) 
                 FWILBIN='iptables';
                 IPPROC='ip_tables*';;
        esac;;
    "3" )
      FWILBIN='iptables';
      IPPROC='ip_table*';;
    * )
      FWILBIN='Unknown';;
esac
[[ $IP4E ]] && IPV='IPV4'
[[ $IP6E ]] && IPV=$IPV'/IPV6'
FWILPROC1=$(ls /proc/net/$IPPROC 2>/dev/null | grep -c ip_tables)
FWILPROC2=$(which $FWILBIN 2>/dev/null | grep -c $FWILBIN)
if [ $FWILPROC1 -ne 0 -a $FWILPROC2 -ne 0 ] ; then FWILSTAT="IP_Tables"; else FWILSTAT="None"; fi
#
if [ $FWILBIN == "iptables" ]
then
    $FWILBIN -L | grep -i chain > $TMPFD/$TMPFL1
    for C in `awk ' { print $2 } ' $TMPFD/$TMPFL1`
    do
      if [ $C == "INPUT" ]  
      then
          if [ `$FWILBIN -L $C | grep -wc 'all\|tcp\|udp'` -gt 0 ] ; then FWIN="True"; fi
      fi
      if [ $C == "OUTPUT" ]
      then
          if [ `$FWILBIN -L $C | grep -wc 'all\|tcp\|udp'` -gt 0 ] ; then FWOT="True"; fi
      fi
      if [ $C == "FORWARD" ]
      then
          if [ `$FWILBIN -L $C | grep -wc 'all\|tcp\|udp'` -gt 0 ] ; then FWFW="True"; fi
      fi
    done
fi
#
FWR=$($FWILBIN -L | sed '/^$/d' | grep -iv chain | grep -ivc "target\|source\|destination")
[[ $FWR -gt 0 ]] && [[ $FWIN == "True" ]] && FWON='On'
#
printf "%12s %15s %20s %16s\n" "OS" "Kernel" "Release" "Distribution" >> $TMPFD/$AUDITFL
printf "%12s %15s %20s %16s\n\n" "$OPSYS" "$OPKER" "$OPKRL" "$OPDST" >> $TMPFD/$AUDITFL
#
printf "%25s %25s %20s\n" "Hostname" "Domain" "EasyAdmin Utilites" >> $TMPFD/$AUDITFL
printf "%25s %25s %20s\n\n"  "$HSTNM" "$DOMAIN" "$License-v$UVersion" >> $TMPFD/$AUDITFL
#
printf "%15s%12s%20s%20s %25s\n" "Net Interfaces" "Forwarding" "Masquerading" "1st Name Srv" "Default Gateway" >> $TMPFD/$AUDITFL
printf "%15s%12s%20s%20s %25s\n\n" "$TNIC" "$IPFW" "$IPMQ" "$NMSR" "$DFGW" >> $TMPFD/$AUDITFL
#
printf "%14s%14s%14s%14s%14s%14s%14s\n" "FW Binary" "FW Status" "FW In" "FW Out" "FW Forward" "FW Masq" "IPV">> $TMPFD/$AUDITFL
printf "%14s%14s%14s%14s%14s%14s%14s\n" "$FWILBIN" "$FWON" "$FWIN" "$FWOT" "$FWFW" "$FWMQ" "$IPV">> $TMPFD/$AUDITFL
#
#
T=$(ls -l /dev/hd[abcdefghijklmnop] 2>/dev/null | wc -l)
if (( $T > 0 )) ; then HDTYPE=1; fi
HDD=$(( HDD + T ))
T=$(ls -l /dev/sd[abcdefghijklmnop] 2>/dev/null | wc -l)
if (( $T > 0 )) ; then SDTYPE=1; fi
SDD=$(( HDD + T ))
TOTHD=$(( HDD + SDD ))
T=$(ls -l /dev/sr[0-9] 2>/dev/null | wc -l)
RCD=$(( RCD + T ))
#
printf "\n%25s %25s %15s %15s\n" "Hardware-Controllers" "Disk's at boot time" "HDD-Disks" "Removable-Disks" >> $TMPFD/$AUDITFL
printf "%25s %25s %15s %15s\n\n" "$HWRCTL" "$CNTDSK" "$TOTHD" "$RCD"  >> $TMPFD/$AUDITFL
#
printf "%15s %12s %20s %8s\n" "DISK" "Capacity" "Partitions: Actual" "Mounted" >> $TMPFD/$AUDITFL
if (( HDD > 0 )) ; then 
   for (( i=0; i<HDD; i++ ))
   do
     DISK="hd${DA[$i]}"
     DSKTMPFL="$DISK-file"
     fdisk -l /dev/$DISK 2>/dev/null | grep $DISK >> $TMPFD/$DSKTMPFL
     DISKCAP=$(grep "Disk /dev/$DISK" $TMPFD/$DSKTMPFL | awk ' { print $3, $4 } ' | sed 's/,//')
     DPART=0
     T=$(ls -l /dev/$DISK[0-9] 2>/dev/null | wc -l)
     DPART=$(( DPART + T ))
     T=$(ls -l /dev/$DISK[0-9][0-9] 2>/dev/null | wc -l)
     DPART=$(( DPART + T ))
     MPART=$(mount | grep /dev/$DISK 2>/dev/null | wc -l)
     printf "%15s %12s %20s %8s\n" "$DISK" "$DISKCAP" "$DPART" "$MPART" >> $TMPFD/$AUDITFL
   done
fi
#
if (( SDD > 0 )) ; then
   for (( i=0; i<SDD; i++ ))
   do
     DISK="sd${DA[$i]}"
     DSKTMPFL="$DISK-file"
     fdisk -l /dev/$DISK 2>/dev/null | grep $DISK >> $TMPFD/$DSKTMPFL
     DISKCAP=$(grep "Disk /dev/$DISK" $TMPFD/$DSKTMPFL | awk ' { print $3, $4 } ' | sed 's/,//')
     DPART=0
     T=$(ls -l /dev/$DISK[0-9] 2>/dev/null | wc -l)
     DPART=$(( DPART + T ))
     T=$(ls -l /dev/$DISK[0-9][0-9] 2>/dev/null | wc -l)
     DPART=$(( DPART + T ))
     MPART=$(mount | grep -i /dev/$DISK 2>/dev/null | wc -l)
     printf "%15s %12s %20s %8s\n" "$DISK" "$DISKCAP" "$DPART" "$MPART" >> $TMPFD/$AUDITFL
   done
fi
#
mount | grep ^/dev/ > $TMPFD/$TMPFL1
grep -v ^# /etc/fstab | grep -v "tmpfs\|devpts\|sysfs\|proc" | sed '/^$/d' > $TMPFD/$TMPFL2
df -h | grep ^/dev/ > $TMPFD/$TMPFL3
#
#
printf "\n%5s %s\n" " " "Filesystem      Size  Used Avail Use% Mounted-on"  >> $TMPFD/$AUDITFL
awk ' { printf "%5s %s\n", " ", $0 } ' $TMPFD/$TMPFL3  >> $TMPFD/$AUDITFL
#
free -m > $TMPFD/$TMPFL1
echo >> $TMPFD/$AUDITFL
printf "%s\n" "Type     Total(Mb)       Used       Free     Shared    Buffers     Cached" >> $TMPFD/$AUDITFL
grep -iv 'total\|free\|used\|shared\|buffers\|cache' $TMPFD/$TMPFL1 >> $TMPFD/$AUDITFL
echo >> $TMPFD/$AUDITFL
grep -iv 'total\|free\|used\|shared\|mem\|swap' $TMPFD/$TMPFL1 >> $TMPFD/$AUDITFL
echo >> $TMPFD/$AUDITFL
#
cat $TMPFD/$AUDITFL
cleanup 1>/dev/null
echo "done over"
##################################################################
